home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / listings / v_13_11 / phillip2 / segments.c < prev    next >
C/C++ Source or Header  |  1993-06-29  |  64KB  |  2,212 lines

  1.  
  2.    /*************************** 
  3.    * 
  4.    *   segments.c 
  5.    *   COMPOSITE FILE COMPRISING: 
  6.    *   segment.c 
  7.    *   segment2.c 
  8.    * 
  9.    ***************************\ 
  10.  
  11.  
  12.  
  13.    /*********************************************
  14.    *
  15.    *       file d:\cips\segment.c
  16.    *
  17.    *       Functions: This file contains
  18.    *           adaptive_threshold_segmentation
  19.    *           append_stack_files
  20.    *           copy_stack_files
  21.    *           find_peaks
  22.    *           find_valley_point
  23.    *           grow
  24.    *           insert_into_peaks
  25.    *           insert_into_deltas
  26.    *           label_and_check_neighbors
  27.    *           manual_threshold_segmentation
  28.    *           peak_threshold_segmentation
  29.    *           peaks_high_low
  30.    *           push_data_onto_stack_file
  31.    *           pop_data_off_of_stack_file
  32.    *           threshold_image_array
  33.    *           valley_high_low
  34.    *           valley_threshold_segmentation
  35.    *           get_segmentation_options
  36.    *
  37.    *       Purpose:
  38.    *           These functions are part of histogram
  39.    *           based image segmentation.
  40.    *
  41.    *       External Calls:
  42.    *          wtiff.c - round_off_image_size
  43.    *                    create_file_if_needed
  44.    *                    write_array_into_tiff_image
  45.    *          tiff.c - read_tiff_header
  46.    *          rtiff.c - read_tiff_image
  47.    *          numcvrt.c - get_integer
  48.    *                      get_short
  49.    *
  50.    *       Modifications:
  51.    *           October 1992 - created
  52.    *
  53.    ************************************************/
  54.  
  55.  
  56. #include "cips.h"
  57.  
  58.  
  59.    /**************************************************
  60.    *
  61.    *   manual_threshold_segmentation(...
  62.    *
  63.    *   This function segments an image using thresholding
  64.    *   given the hi and low values of the threshold
  65.    *   by the calling routine.  It reads in an image
  66.    *   and writes the result to the output image.
  67.    *
  68.    *   If the segment parameter is 0, you only
  69.    *   threshold the array - you do not segment.
  70.    *
  71.    ***************************************************/
  72.  
  73. manual_threshold_segmentation(in_name, out_name,
  74.                               the_image, out_image,
  75.                               il, ie, ll, le,
  76.                               hi, low, value, segment)
  77.    char   in_name[], out_name[];
  78.    int    il, ie, ll, le, segment;
  79.    short  hi, low, the_image[ROWS][COLS],
  80.           out_image[ROWS][COLS], value;
  81. {
  82.    int    length, width;
  83.    struct tiff_header_struct image_header;
  84.  
  85.    create_file_if_needed(in_name, out_name, out_image);
  86.  
  87.    read_tiff_image(in_name, the_image, il, ie, ll, le);
  88.    threshold_image_array(the_image, out_image,
  89.             hi, low, value);
  90.    if(segment == 1)
  91.       grow(out_image, value);
  92.    write_array_into_tiff_image(out_name, out_image,
  93.                                il, ie, ll, le);
  94.  
  95. }  /* ends manual_threshold_segmentation */
  96.  
  97.  
  98.  
  99.  
  100.  
  101.    /************************************************
  102.    *
  103.    *   peak_threshold_segmentation(...
  104.    *
  105.    *   This function segments an image using
  106.    *   thresholding.  It uses the histogram peaks
  107.    *   to find the hi and low values of the
  108.    *   threshold.
  109.    *
  110.    *   If the segment parameter is 0, you only
  111.    *   threshold the array - you do not segment.
  112.    *
  113.    *************************************************/
  114.  
  115. peak_threshold_segmentation(in_name, out_name,
  116.                             the_image, out_image,
  117.                             il, ie, ll, le,
  118.                             value, segment)
  119.    char   in_name[], out_name[];
  120.    int    il, ie, ll, le, segment;
  121.    short  the_image[ROWS][COLS],
  122.           out_image[ROWS][COLS], value;
  123. {
  124.    int      length, peak1, peak2, width;
  125.    short    hi, low;
  126.    struct   tiff_header_struct image_header;
  127.    unsigned long histogram[GRAY_LEVELS+1];
  128.  
  129.    create_file_if_needed(in_name, out_name, out_image);
  130.  
  131.    read_tiff_image(in_name, the_image, il, ie, ll, le);
  132.    zero_histogram(histogram);
  133.    calculate_histogram(the_image, histogram);
  134.    smooth_histogram(histogram);
  135.    find_peaks(histogram, &peak1, &peak2);
  136.    peaks_high_low(histogram, peak1, peak2,
  137.                   &hi, &low);
  138.    threshold_image_array(the_image, out_image,
  139.                          hi, low, value);
  140.    if(segment == 1)
  141.       grow(out_image, value);
  142.    write_array_into_tiff_image(out_name, out_image,
  143.                                il, ie, ll, le);
  144.  
  145. }  /* ends peak_threshold_segmentation */
  146.  
  147.  
  148.  
  149.  
  150.  
  151.    /************************************************
  152.    *
  153.    *   valley_threshold_segmentation(...
  154.    *
  155.    *   This function segments an image using
  156.    *   thresholding.  It uses the histogram valleys
  157.    *   to find the hi and low values of the
  158.    *   threshold.
  159.    *
  160.    *   If the segment parameter is 0, you only
  161.    *   threshold the array - you do not segment.
  162.    *
  163.    *************************************************/
  164.  
  165. valley_threshold_segmentation(in_name, out_name,
  166.                               the_image, out_image,
  167.                               il, ie, ll, le,
  168.                               value, segment)
  169.    char   in_name[], out_name[];
  170.    int    il, ie, ll, le, segment;
  171.    short  the_image[ROWS][COLS],
  172.           out_image[ROWS][COLS], value;
  173. {
  174.    int      length, peak1, peak2, width;
  175.    short    hi, low;
  176.    struct   tiff_header_struct image_header;
  177.    unsigned long histogram[GRAY_LEVELS+1];
  178.  
  179.    create_file_if_needed(in_name, out_name, out_image);
  180.  
  181.    read_tiff_image(in_name, the_image, il, ie, ll, le);
  182.    zero_histogram(histogram);
  183.    calculate_histogram(the_image, histogram);
  184.    smooth_histogram(histogram);
  185.    find_peaks(histogram, &peak1, &peak2);
  186.    valley_high_low(histogram, peak1, peak2,
  187.                    &hi, &low);
  188.    threshold_image_array(the_image, out_image,
  189.                          hi, low, value);
  190.    if(segment == 1)
  191.       grow(out_image, value);
  192.    write_array_into_tiff_image(out_name, out_image,
  193.                                il, ie, ll, le);
  194.  
  195. }  /* ends valley_threshold_segmentation */
  196.  
  197.  
  198.  
  199.  
  200.  
  201.    /************************************************
  202.    *
  203.    *   adaptive_threshold_segmentation(...
  204.    *
  205.    *   This function segments an image using
  206.    *   thresholding.  It uses two passes
  207.    *   to find the hi and low values of the
  208.    *   threshold.  The first pass uses the peaks
  209.    *   of the histogram to find the hi and low
  210.    *   threshold values.  It thresholds the image
  211.    *   using these hi lows and calculates the means
  212.    *   of the object and background.  Then we use
  213.    *   these means as new peaks to calculate new
  214.    *   hi and low values.  Finally, we threshold
  215.    *   the image again using these second hi low
  216.    *   hi low values.
  217.    *
  218.    *   If the segment parameter is 0, you only
  219.    *   threshold the array - you do not segment.
  220.    *
  221.    *************************************************/
  222.  
  223. adaptive_threshold_segmentation(in_name, out_name,
  224.                                 the_image, out_image,
  225.                                 il, ie, ll, le,
  226.                                 value, segment)
  227.    char   in_name[], out_name[];
  228.    int    il, ie, ll, le, segment;
  229.    short  the_image[ROWS][COLS],
  230.           out_image[ROWS][COLS], value;
  231. {
  232.    int      length, peak1, peak2, width;
  233.    short    background, hi, low, object;
  234.    struct   tiff_header_struct image_header;
  235.    unsigned long histogram[GRAY_LEVELS+1];
  236.  
  237.    create_file_if_needed(in_name, out_name, out_image);
  238.  
  239.    read_tiff_image(in_name, the_image, il, ie, ll, le);
  240.    zero_histogram(histogram);
  241.    calculate_histogram(the_image, histogram);
  242.    smooth_histogram(histogram);
  243.    find_peaks(histogram, &peak1, &peak2);
  244.    peaks_high_low(histogram, peak1, peak2,
  245.                   &hi, &low);
  246.    threshold_and_find_means(the_image, out_image,
  247.                             hi, low, value,
  248.                             &object, &background);
  249.    peaks_high_low(histogram, object, background,
  250.                   &hi, &low);
  251.    threshold_image_array(the_image, out_image,
  252.                          hi, low, value);
  253.    if(segment == 1)
  254.       grow(out_image, value)